-
Notifications
You must be signed in to change notification settings - Fork 1
/
database queries.sql
274 lines (229 loc) · 9.23 KB
/
database queries.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
-- ################################# Students #################################
-- ------------------------- Register students -------------------------
-- inserting into students table
INSERT INTO students (id, password)
values (${data.studentID}, '${data.password}');
-- inserting into personalData table
INSERT INTO personalData (studentID, firstName, lastName, birthday, gender)
values (${data.studentID}, '${data.firstName}', '${data.lastName}', '${data.birthday}',
'${data.gender}');
-- inserting into contactData table
INSERT INTO contactData (studentID, email, phoneNumber, address)
values (${data.studentID}, '${data.email}', '${data.phoneNumber}', '${data.address}');
-- inserting into academicData table
INSERT INTO academicData (studentID, level, GPA)
values (${data.studentID}, ${data.level}, ${data.gpa});
-- deleting from unregisteredStudents table
delete
from unregisteredStudents
where id = ${data.studentID};
-- ------------------------- login -------------------------
-- collect student ID and password to verify the login
select *
from students
where id = ${studentID};
-- collect all student data to show it in his profile page
select students.id as studentID,
students.password as password,
personalData.firstName as firstName,
personalData.lastName as lastName,
personalData.birthday as birthday,
personalData.gender as gender,
contactData.email as email,
contactData.phoneNumber as phoneNumber,
contactData.address as address,
academicData.level as level,
academicData.GPA as gpa
from students
join personalData on students.id = personalData.studentID
join contactData on students.id = contactData.studentID
join academicData on students.id = academicData.studentID
where students.id = ${studentID};
-- ------------------------- update students data -------------------------
-- update student's password
update students
set password = '${data.password}'
where id = ${data.studentID};
-- update student's personal data
update personalData
set firstName = '${data.firstName}',
lastName = '${data.lastName}'
where studentID = ${data.studentID};
-- update student's contact data
update contactData
set email = '${data.email}',
phoneNumber = '${data.phoneNumber}',
address = '${data.address}'
where studentID = ${data.studentID};
-- update students academic data
update academicData
set level = '${data.level}',
gpa = '${data.gpa}'
where studentID = ${data.studentID};
-- ------------------------- courses -------------------------
-- select all existing courses materials
select id, name, code
from courses;
-- collect all studentCourses data of a student
select studentCourse.id as courseID,
courses.name as courseName,
courses.code as courseCode,
studentCourse.grade as courseGrade,
studentCourse.level as courseLevel,
studentCourse.semester as courseSemester
from students
join studentCourse on students.id = studentCourse.studentID
join courses on studentCourse.courseID = courses.id
where students.id = ${req.session.user.id}
order by courseLevel, courseSemester;
-- check a course belongs to a student
select studentID
from studentCourse
where studentID = ${studentId}
and courseID = ${courseID};
-- inserting a new course into studentCourse
INSERT INTO studentCourse (studentID, courseID, grade, level, semester)
values (${studentID}, ${data.courseID}, ${data.courseGrade},
${data.courseLevel}, ${data.courseSemester});
-- collect studentCourse information
select courses.id as courseID,
courses.code as courseCode,
courses.name as courseName,
studentCourse.grade as courseGrade,
studentCourse.level as courseLevel,
studentCourse.semester as courseSemester
from studentCourse
join courses on studentCourse.courseID = courses.id
where studentCourse.id = ${studentCourseID};
-- update studentCourse information
update studentCourse
set courseID = '${data.courseID}',
grade = ${data.courseGrade},
level = ${data.courseLevel},
semester = ${data.courseSemester}
where id = ${studentCourseID};
-- delete studentCourse
delete
from studentCourse
where id = ${studentCourseID};
-- ------------------------- notes -------------------------
-- collect all notes data of a studentCourse
select notes.id as noteID,
if(char_length(notes.title) > 45, concat(substr(notes.title, 1, 45), '...'),
notes.title) as noteTitle,
if(char_length(notes.content) > 100,
concat(substr(notes.content, 1, 100), '...'),
notes.content) as noteContent
from notes
join studentCourse on notes.studentCourseID = studentCourse.id
where studentCourse.id = ${studentCourseID};
-- search in studentCourse notes
select notes.id as noteID,
if(char_length(notes.title) > 45, concat(substr(notes.title, 1, 45), '...'),
notes.title) as noteTitle,
if(char_length(notes.content) > 100,
concat(substr(notes.content, 1, 100), '...'),
notes.content) as noteContent
from notes
join studentCourse on notes.studentCourseID = studentCourse.id
where studentCourse.id = ${studentCourseID}
and (notes.title like '%${searchWord}%'
or notes.content like '%${searchWord}%');
-- add new note to a studentCourse
insert into notes (studentCourseID, title, content)
values (${req.params.studentCourseID}, '${req.body.title}', '${req.body.content}');
-- collect note information
select title, content
from notes
where notes.id = ${noteID};
-- update note data
update notes
set title = '${req.body.title}',
content = '${req.body.content}'
where id = ${req.params.noteID};
-- delete note
delete
from notes
where id = ${noteID};
-- ################################# Admins #################################
-- ------------------------- Login -------------------------
-- collect admin ID and password to verify the login
select *
from admins
where id = '${adminID}';
-- ------------------------- Admin panel -------------------------
-- Collect admin information to show it in admin panel
select admin_db.personalData.firstName as firstName,
admin_db.personalData.lastName as lastName,
admin_db.personalData.phoneNumber as phoneNumber,
admin_db.personalData.email as email
from admins
join personalData on admins.id = admin_db.personalData.adminID
where admins.id = '${req.session.user.id}';
-- Check if a range of IDs exist in students table
select id
from students
where id between ${startID} and ${endID};
-- Check if a range of IDs exist in unregisteredStudents table
select id
from unregisteredStudents
where id between ${startID} and ${endID};
-- Insert a range of IDs to unregisteredStudents table
INSERT INTO unregisteredStudents (id)
VALUES ?;
-- ------------------------- Show students data -------------------------
-- Show students that their IDs are in a range of IDs
select students.id as studentID,
personalData.firstName as firstName,
personalData.lastName as lastName,
academicData.level as level,
academicData.GPA as gpa
from students
join personalData on students.id = personalData.studentID
join contactData on students.id = contactData.studentID
join academicData on students.id = academicData.studentID
where students.id between ${startID} and ${endID};
-- Show students that their level is equal to a specific level
select students.id as studentID,
personalData.firstName as firstName,
personalData.lastName as lastName,
academicData.level as level,
academicData.GPA as gpa
from students
join personalData on students.id = personalData.studentID
join contactData on students.id = contactData.studentID
join academicData on students.id = academicData.studentID
where academicData.level = ${level};
-- Show students that their GPAs are in a range of GPAs
select students.id as studentID,
personalData.firstName as firstName,
personalData.lastName as lastName,
academicData.level as level,
academicData.GPA as gpa
from students
join personalData on students.id = personalData.studentID
join contactData on students.id = contactData.studentID
join academicData on students.id = academicData.studentID
where academicData.gpa between ${startGPA} and ${endGPA};
-- ------------------------- Edit students data -------------------------
-- Get the column data type
SELECT data_type
FROM information_schema.columns
WHERE table_schema = 'student_db'
AND table_name = '${tableName}'
AND column_name = '${columnName}';
-- Update a table with a column of an integer data type
update ${data.tableName}
set ${data.columnName} = ${data.updateValue}
where studentID between ${data.startID} and ${data.endID};
-- Update a table with a column of a non integer data type
update ${data.tableName}
set ${data.columnName} = '${data.updateValue}'
where studentID between ${data.startID} and ${data.endID};
-- Delete students
delete
from students
where id between ${startID} and ${endID};
-- Add new students
INSERT INTO unregisteredStudents (id)
VALUES ?;